//
//  CJPictorialMainTableViewCell.m
//  KaiJing
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "CJPictorialMainTableViewCell.h"
#import "UIButton+WebCache.h"

#define kButtonSpace 5  //button 之间的间距

@interface CJPictorialMainTableViewCell ()

@property (nonatomic, strong) UIButton *button; // 九宫格
@property (nonatomic, strong) NSString *str;
@end


@implementation CJPictorialMainTableViewCell




- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    
        //九宫格button
        for (int i = 0; i < 9; i++) {
            
            _button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            _button.frame = CGRectMake(0 + ((kScreenWidth - 10) / 3 + 5) * (i % 3)  , 10 + ((kScreenWidth - 10) / 3 + 5) * (i / 3), (kScreenWidth - 10) / 3, (kScreenWidth - 10) / 3);
            _button.tag = 2000 + i;
            [_button addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)];
            [self.contentView addSubview:_button];
     
            
        }
        
        //标题
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _button.bottom + 10, kScreenWidth - 20, 40)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.numberOfLines = -1;
        _titleLabel.font = [UIFont systemFontOfSize:18];
        [self.contentView addSubview:_titleLabel];
        
        
        //内容
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.bottom + 10, kScreenWidth - 20, 70)];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.numberOfLines = -1;
        _contentLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_contentLabel];
        
        
        //下面视图集合
        _bottomView = [[CJCellBottomView alloc] initWithFrame:CGRectMake(0, _contentLabel.bottom + 5, kScreenWidth, 25)];
        [self.contentView addSubview:_bottomView];
        
        //点赞
        [_bottomView.praiseButton setImage:[UIImage imageNamed:@"parise_norml"] forState:(UIControlStateNormal)];
        [_bottomView.praiseButton setImage:[UIImage imageNamed:@"parise_select"] forState:(UIControlStateSelected)];
        _bottomView.praiseLabel.text = @"赞";
        //评论
        [_bottomView.commentButton setImage:[UIImage imageNamed:@"comment"] forState:(UIControlStateNormal)];
        _bottomView.commentLabel.text = @"评论";
        
        //更多
        [_bottomView.moreButton setImage:[UIImage imageNamed:@"more"] forState:(UIControlStateNormal)];
        
    }
    return self;
}



//重写model方法
- (void)setModel:(CJPictorialMainModel *)model {
    _model = model;
 
        //九宫格
        for (int i = 0; i < 9; i++) {
            
            UIButton *button = (UIButton *)[self.contentView viewWithTag:2000 + i];
            CJPictorialMainImagelistModel *listModel = model.imagelist[i];
            [button setImage:nil forState:(UIControlStateNormal)];
            NSString *urlStr = [NSString stringWithFormat:@"http://morguo.com/%@",listModel.thumb];
            [button sd_setImageWithURL:[NSURL URLWithString:urlStr] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"recommend_selected"]];
    
            
        }

    _titleLabel.text = model.subject;
    _contentLabel.text = model.intro;
    _str = model.subject;
    if (model.isliked) {
        _bottomView.praiseButton.selected = YES;

        
    } else {
        _bottomView.praiseButton.selected = NO;
    }
    
  
 
}



//button点击方法
- (void)clickButton:(UIButton *)button {
    
    
    [_delegate clickCell:self buttonTag:button.tag - 2000];
    
    
}








- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
